package org.jeecg.modules.wlhy.bill.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wlhy.bill.entity.HyCustomerBill;
import org.jeecg.modules.wlhy.bill.entity.HyCustomerBillDTO;
import org.jeecg.modules.wlhy.bill.entity.HyShippingNoteBill;
import org.jeecg.modules.wlhy.bill.service.IHyCustomerBillService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerConfig;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerConfigService;
import org.jeecg.modules.wlhy.setting.service.IHySettingService;
import org.jeecg.modules.wlhy.shipping.entity.HyShippingNote;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.jeecg.modules.wlhy.utils.DataMaskingUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

 /**
 * @Description: hy_customer_bill
 * @Author: jeecg-boot
 * @Date:   2021-01-20
 * @Version: V1.0
 */
@Api(tags="hy_customer_bill")
@RestController
@RequestMapping("/bill/hyCustomerBill")
@Slf4j
public class HyCustomerBillController extends JeecgController<HyCustomerBill, IHyCustomerBillService> {

	 @Autowired
	 private IHyCustomerBillService hyCustomerBillService;

	 @Autowired
	 private UserUtil userUtil;

	 @Autowired
	 private IHyShippingNoteService hyShippingNoteService;

	 @Autowired
	 private IHyCustomerConfigService customerConfigService;

	 @Resource
	 private IHySettingService settingService;

	/**
	 * 分页列表查询
	 *
	 * @param hyCustomerBill
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="hy_customer_bill-分页列表查询", notes="hy_customer_bill-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCustomerBillDTO hyCustomerBill,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<HyCustomerBillDTO> queryWrapper = QueryGenerator.initQueryWrapper(hyCustomerBill, req.getParameterMap());
		QueryWrapper<HyCustomerBillDTO> queryWrapper = new QueryWrapper();
		HyCustomer customer=null;
		if(!userUtil.isSys()){
			 customer = userUtil.getCustomer();
			if(customer == null){
				return Result.error("无权限查看");
			}

			HyCustomerConfig customerConfig = customerConfigService.getByCustomerId(customer.getId());

			if(customerConfig!=null) {
				if (!"1".equals(customerConfig.getIzShowDrvierFee())) {
					queryWrapper.ne("hy_customer_bill.bill_type","8");
					queryWrapper.ne("hy_customer_bill.bill_type","9");
				}
			}
			queryWrapper.eq("hy_customer_bill.customer_id",customer.getId());
		}

		String shippingNoteNumber = req.getParameter("shippingNoteNumber");
		if(StringUtils.isNotBlank(shippingNoteNumber)){
			HyShippingNote shippingNote = hyShippingNoteService.getByShippingNoteNumer(shippingNoteNumber);
			if(shippingNote != null){
				queryWrapper.eq("hy_customer_bill.shipping_id",shippingNote.getId());
			}else {
				return Result.error("无结果");
			}
		}





		String payDtBegin = req.getParameter("payDt_begin");
		String payDtEnd = req.getParameter("payDt_end");

		if(StringUtils.isNotEmpty(payDtBegin)){
			queryWrapper.ge("hy_customer_bill.pay_dt",payDtBegin + " 00:00:00");
		}
		if(StringUtils.isNotEmpty(payDtEnd )){
			queryWrapper.le("hy_customer_bill.pay_dt",payDtEnd + " 23:59:59" );
		}

		setQueryWrapper(hyCustomerBill, queryWrapper);

		Page<HyCustomerBillDTO> page = new Page<HyCustomerBillDTO>(pageNo, pageSize);
		IPage<HyCustomerBillDTO> pageList = hyCustomerBillService.getPageList(page, queryWrapper);

		//如果开启系统脱敏
		String dataMaskingFlag = this.settingService.getSettingValueByTag("data_masking");
		if("1".equals(dataMaskingFlag)) {
			for (HyCustomerBillDTO dto : pageList.getRecords()) {
				dto.setBillNum(DataMaskingUtil.bankNumberMasking(dto.getBillNum()));
				dto.setTelephone(DataMaskingUtil.phoneMasking(dto.getTelephone()));
				dto.setDbaccno(DataMaskingUtil.bankNumberMasking(dto.getDbaccno()));
			}
		}

		return Result.OK(pageList);
	}

	 private void setQueryWrapper(HyCustomerBillDTO hyCustomerBill,QueryWrapper<HyCustomerBillDTO> queryWrapper){


		 if(StringUtils.isNotEmpty(hyCustomerBill.getDriverName())){
			 queryWrapper.like("d.driver_name",hyCustomerBill.getDriverName());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getTelephone())){
			 queryWrapper.like("d.telephone",hyCustomerBill.getTelephone());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getVehicleNumber())){
			 queryWrapper.like("v.vehicle_number",hyCustomerBill.getVehicleNumber());
		 }



//		 if(StringUtils.isNotEmpty(hyCustomerBill.getPayDt())){
//			 queryWrapper.eq("hy_customer_bill.bill_num",hyCustomerBill.getBillNum());
//		 }

//		 pay_channel

		 if(StringUtils.isNotEmpty(hyCustomerBill.getShippingId())){
			 queryWrapper.eq("hy_customer_bill.shipping_id",hyCustomerBill.getShippingId());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getDbaccno())){
			 queryWrapper.eq("hy_customer_bill.dbaccno",hyCustomerBill.getDbaccno());
		 }
		 if(StringUtils.isNotEmpty(hyCustomerBill.getDbaccname())){
			 queryWrapper.eq("hy_customer_bill.dbaccname",hyCustomerBill.getDbaccname());
		 }
		 if(StringUtils.isNotEmpty(hyCustomerBill.getCraccno())){
			 queryWrapper.eq("hy_customer_bill.craccno",hyCustomerBill.getCraccno());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getCraccname())){
			 queryWrapper.eq("hy_customer_bill.craccname",hyCustomerBill.getCraccname());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getCrlogaccno())){
			 queryWrapper.eq("hy_customer_bill.crlogaccno",hyCustomerBill.getCrlogaccno());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getCompanyId())){
			 queryWrapper.eq("hy_customer_bill.company_id",hyCustomerBill.getCompanyId());
		 }
		 if(StringUtils.isNotEmpty(hyCustomerBill.getPayChannel())){
			 queryWrapper.eq("hy_customer_bill.pay_channel",hyCustomerBill.getPayChannel());
		 }
		 if(StringUtils.isNotEmpty(hyCustomerBill.getCustomerId())){
			 queryWrapper.eq("hy_customer_bill.customer_id",hyCustomerBill.getCustomerId());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getBillNum())){
			 queryWrapper.eq("hy_customer_bill.bill_num",hyCustomerBill.getBillNum());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getBillType())){
			 queryWrapper.eq("hy_customer_bill.bill_type",hyCustomerBill.getBillType());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getCustomerId())){
			 queryWrapper.eq("hy_customer_bill.customer_id",hyCustomerBill.getCustomerId());
		 }

		 if(StringUtils.isNotEmpty(hyCustomerBill.getCreateTime_begin())){
			 queryWrapper.ge("hy_customer_bill.create_time",hyCustomerBill.getCreateTime_begin()+" 00:00:00");
		 }
		 if(StringUtils.isNotEmpty(hyCustomerBill.getCreateTime_end() )){
			 queryWrapper.le("hy_customer_bill.create_time",hyCustomerBill.getCreateTime_end()+" 23:59:59" );
		 }
	 }

//	 /**
//	 *   添加
//	 *
//	 * @param hyCustomerBill
//	 * @return
//	 */
//	@AutoLog(value = "hy_customer_bill-添加")
//	@ApiOperation(value="hy_customer_bill-添加", notes="hy_customer_bill-添加")
//	@PostMapping(value = "/add")
//	public Result<?> add(@RequestBody HyCustomerBill hyCustomerBill) {
//		hyCustomerBillService.save(hyCustomerBill);
//		return Result.OK("添加成功！");
//	}
//
//	/**
//	 *  编辑
//	 *
//	 * @param hyCustomerBill
//	 * @return
//	 */
//	@AutoLog(value = "hy_customer_bill-编辑")
//	@ApiOperation(value="hy_customer_bill-编辑", notes="hy_customer_bill-编辑")
//	@PutMapping(value = "/edit")
//	public Result<?> edit(@RequestBody HyCustomerBill hyCustomerBill) {
//		hyCustomerBillService.updateById(hyCustomerBill);
//		return Result.OK("编辑成功!");
//	}
//
//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "hy_customer_bill-通过id删除")
//	@ApiOperation(value="hy_customer_bill-通过id删除", notes="hy_customer_bill-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		hyCustomerBillService.removeById(id);
//		return Result.OK("删除成功!");
//	}
//
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "hy_customer_bill-批量删除")
//	@ApiOperation(value="hy_customer_bill-批量删除", notes="hy_customer_bill-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.hyCustomerBillService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value="hy_customer_bill-通过id查询", notes="hy_customer_bill-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		HyCustomerBill hyCustomerBill = hyCustomerBillService.getById(id);
		if(hyCustomerBill==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(hyCustomerBill);
	}


//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, HyCustomerBill hyCustomerBill) {
//		return super.exportXls(request, hyCustomerBill, HyCustomerBill.class, "资金流水单");
//    }

	 @RequestMapping(value = "/exportXlsDTO")
	 public ModelAndView exportXlsDTO(HttpServletRequest request, HyCustomerBillDTO hyCustomerBillDTO) {
		 QueryWrapper<HyCustomerBillDTO> queryWrapper = new QueryWrapper();
		 boolean isSys = userUtil.isSys();
		 String customerId = null;
		 if(!isSys) {
			 HyCustomer customer = userUtil.getCustomer();
			 if (customer == null){
				 throw new JeecgBootException("无权下载");
			 }
			 customerId = customer.getId();
			 queryWrapper.eq("hy_customer_bill.customer_id", customerId);

			 HyCustomerConfig customerConfig = customerConfigService.getByCustomerId(customer.getId());

			 if(customerConfig!=null) {
				 if (!"1".equals(customerConfig.getIzShowDrvierFee())) {
					 queryWrapper.ne("hy_customer_bill.bill_type","8");
					 queryWrapper.ne("hy_customer_bill.bill_type","9");
				 }
			 }
		 }
		 String payDtBegin = request.getParameter("payDt_begin");
		 String payDtEnd = request.getParameter("payDt_end");

		 if(StringUtils.isNotEmpty(payDtBegin)){
			 queryWrapper.ge("hy_customer_bill.pay_dt",payDtBegin + " 00:00:00");
		 }
		 if(StringUtils.isNotEmpty(payDtEnd )){
			 queryWrapper.le("hy_customer_bill.pay_dt",payDtEnd + " 23:59:59" );
		 }

		 setQueryWrapper(hyCustomerBillDTO, queryWrapper);
		 List<HyCustomerBillDTO> list = hyCustomerBillService.getList(queryWrapper);
		 if(!isSys) {
			 for (HyCustomerBillDTO customerBillDTO : list) {
				 if ("3".equals(customerBillDTO.getOrderMode())) {
					if(!StringUtils.equals(customerId, customerBillDTO.getAgentId())){
						customerBillDTO.setCostPayShipping(null);
					}
					 HyShippingNote shippingNote = hyShippingNoteService.getById(customerBillDTO.getShippingId());
					 customerBillDTO.setAmt(shippingNote.getAgentFee());
					 customerBillDTO.setCostPayShipping(customerBillDTO.getAmt());
				 }
			 }
		 }
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 String title = "资金流水单";
		 mv.addObject(NormalExcelConstants.FILE_NAME, title);
		 mv.addObject(NormalExcelConstants.CLASS, HyCustomerBillDTO.class);
		 mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", title));
		 mv.addObject(NormalExcelConstants.DATA_LIST, list);
		 return mv;
	 }

//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, HyCustomerBill.class);
//    }

}
